{% extends "admin_base.html" %}
{% load i18n avatar_tags seahub_tags %}
{% load url from future %}

{% block extra_style %}
<style type="text/css">
#left-panel { position:relative; }
</style>
{% endblock %}

{% block left_panel %}
<a class="go-back" title="{% trans "Back to user list" %}"  href="{% url 'sys_useradmin' %}"><span class="icon-chevron-left"></span></a>
<div class="side-info">
    <h3 class="hd">{% trans "Profile" %}</h3>
    {% avatar email 48 %}
    <dl>
        <dt>{% trans "Email" %}</dt>
        <dd>{{ email }}</dd>

        {% if org_name %}
        <dt>{% trans "Organization" %}</dt>
        <dd>{{ org_name }}</dd>
        {%  endif %}

        {% if profile %}
        <dt>{% trans "Name" context "true name" %}</dt>
        <dd>{{ profile.nickname }}</dd>

        <dt>{% trans "Self-introduction" %}</dt>
        <dd>{{ profile.intro }}</dd>
        {% endif %}

        {% if d_profile %}
        <dt>{% trans "Department" %}</dt>
        <dd>{{ d_profile.department }}</dd>

        <dt>{% trans "Telephone" %}</dt>
        <dd>{{ d_profile.telephone }}</dd>
        {% endif %}
    </dl>

    <h3 class="hd">{% trans "Space Used" %}</h3>
    <p>{% trans "Used" %}: {{ space_usage|filesizeformat }} {% if space_quota > 0 %} / {{ space_quota|filesizeformat }} {% endif %}</p>

    {% if CALC_SHARE_USAGE %}
    <p>{% trans "Sharing" %}: {{ share_usage|filesizeformat }} {% if share_quota > 0 %} / {{ share_quota|filesizeformat }} {% endif %}</p>
    {% endif %}

    <a href="#" id="set-quota">{% trans "Set Quota" %}</a>
</div>
<form id="set-quota-form" method="post" class="hide">{% csrf_token %}
    <h3>{% trans "Set user storage limit" %}</h3>
    <input type="hidden" name="email" value="{{ email }}" />
    <input type="text" name="space_quota" /> MB
    {% if CALC_SHARE_USAGE %}
    <br /><input type="text" name="share_quota" placeholder=" Share quota" /> MB<br />
    {% endif %}
    <p class="tip">{% trans "Tip: 0 means default limit" %}</p>
    <p class="error hide"></p>
    <input type="submit" value="{% trans "Submit" %}" class="submit" />
</form>
{% endblock %}

{% block right_panel %}
<div id="tabs" class="tab-tabs">
    <div class="hd ovhd">
        <ul class="tab-tabs-nav fleft">
            <li class="tab"><a href="#owned" class="a">{% trans "Owned Libs" %}</a></li>
            <li class="tab"><a href="#shared" class="a">{% trans "Shared Libs" %}</a></li>
            <li class="tab"><a href="#shared-links" class="a">{% trans "Shared Links" %}</a></li>
        </ul>
    </div>

    <div id="owned">
        {% if owned_repos %}
        <table class="repo-list">
            <tr>
                <th width="4%"><!--icon--></th>
                <th width="30%">{% trans "Name" %}</th>
                <th width="46%">{% trans "Description" %}</th>
                <th width="20%">{% trans "Operations" %}</th>
            </tr>

            {% for repo in owned_repos %}
            <tr>
                <td><img src="{{MEDIA_URL}}img/sync-folder-20.png?t=1387267140" title="{% trans "Read-Write" %}" alt="{% trans "directory icon" %}" /></td>
                <td><a href="{{ SITE_ROOT }}repo/{{ repo.props.id }}/">{{ repo.props.name }}</a></td>
                <td>{{ repo.props.desc }}</td>
                <td data-id="{{ repo.props.id }}" data-name="{{repo.name}}">
                    <div><a href="#" class="repo-delete-btn op-icon vh" title="{% trans "Delete" %}"><img src="{{MEDIA_URL}}img/rm.png" alt="" /></a></div>
                </td>
            </tr>
            {% endfor %}
        </table>
        {% else %}
        <p>{% trans "None" %}</p>
        {% endif %}
    </div>

    <div id="shared">
        {% if in_repos %}
        <table>
            <tr>
                <th width="4%"><!--icon--></th>
                <th width="30%">{% trans "Name" %}</th>
                <th width="26%">{% trans "Share From" %}</th>
                <th width="40%">{% trans "Description" %}</th>
            </tr>

            {% for repo in in_repos %}
            <tr>
                <td><img src="{{MEDIA_URL}}img/sync-folder-20.png?t=1387267140" title="{% trans "Read-Write" %}" alt="library icon" /></td>
                <td><a href="{{ SITE_ROOT }}repo/{{ repo.props.id }}">{{ repo.props.repo_name }}</a></td>
                <td>{{ repo.props.user }}</td>
                <td>{{ repo.props.repo_desc }}</td>
            </tr>
            {% endfor %}
        </table>
        {% else %}
        <p>{% trans "None" %}</p>
        {% endif %}
    </div>

    <div id="shared-links">
        {% if user_shared_links%}
        <table class="sharelink-list">
            <tr>
                <th width="5%"><!--icon--></th>
                <th width="30%">{% trans "Name"%}</th>
                <th width="20%">{% trans "Size"%}</th>
                <th width="20%">{% trans "Type"%}</th>
                <th width="10%">{% trans "Visits"%}</th>
                <th width="15%">{% trans "Operations"%}</th>
            </tr>
            {% for link in user_shared_links %}
            <tr>
                {% if link.is_download %}
                    {% if link.is_file_share_link %}
                    <td class="alc"><img src="{{ MEDIA_URL }}img/file/{{ link.filename|file_icon_filter }}" alt="{% trans "File"%}" /></td>
                    <td>{{ link.filename }}</td>
                    <td>{{ link.file_size|filesizeformat}}</td>
                    {% else %}
                    <td class="alc"><img src="{{ MEDIA_URL }}img/folder-icon-24.png" alt="{% trans "Directory icon"%}" /></td>
                    <td>{{ link.filename }}</td>
                    <td>{{ link.dir_size|filesizeformat}}</td>
                    {% endif %}
                    <td>{% trans "Download" %}</td>
                    <td>{{ link.view_cnt }}</td>
                    <td>
                        <a class="op vh" href="{% url 'remove_shared_link' %}?t={{ link.token }}">{% trans "Remove"%}</a>
                    </td>
                {% else %}
                    <td class="alc"><img src="{{ MEDIA_URL }}img/folder-icon-24.png" alt="{% trans "Directory icon"%}" /></td>
                    <td>{{ link.dir_name }}</td>
                    <td>--</td>
                    <td>{% trans "Upload" %}</td>
                    <td>{{ link.view_cnt }}</td>
                    <td>
                        <a class="op vh" href="{% url 'remove_shared_upload_link' %}?t={{ link.token }}">{% trans "Remove"%}</a>
                    </td>
                {% endif %}
            </tr>
            {% endfor %}
        </table>
        {% else %}
        <p>{% trans "None" %}</p>
        {% endif %}
    </div>
</div>

{% include 'snippets/repo_del_popup.html' %}
{% endblock %}


{% block extra_script %}
<script type="text/javascript">

$('#set-quota').click(function() {
    $("#set-quota-form").modal({appendTo: "#main"});
    return false;
});

$('#set-quota-form .submit').click(function() {
    var form = $('#set-quota-form'),
        form_id = form.attr('id'),
        space_quota = $('input[name="space_quota"]', form).val();

    if (!$.trim(space_quota)) {
        apply_form_error(form_id, "{% trans "Space Quota can't be empty" %}");
        return false;
    }

    data = { 'email': $('input[name="email"]', form).val(), 'space_quota': space_quota };

    {% if CALC_SHARE_USAGE %}
    var share_quota = $('input[name="share_quota"]', form).val();
    if (!$.trim(share_quota)) {
        apply_form_error(form_id, "{% trans "Share Quota can't be empty" %}");
        return false;
    }
    data['share_quota'] = share_quota;
    {% endif %}

    var sb_btn = $(this);
    disable(sb_btn);
    $.ajax({
        url: '{% url 'user_set_quota' email %}',
        type: 'POST',
        dataType: 'json',
        cache: 'false',
        beforeSend: prepareCSRFToken,
        data: data,
        success: function(data) {
            location.reload(true);
        },
        error: function(xhr, textStatus, errorThrown) {
            if (xhr.responseText) {
                apply_form_error(form_id, $.parseJSON(xhr.responseText).error);
            } else {
                apply_form_error(form_id, "{% trans "Failed. Please check the network." %}");
            }
            enable(sb_btn);
        }
    });
    return false;
});

{% include 'snippets/repo_del_js.html' %}
</script>
{% endblock %}
